package com.nidu.demo.role.gatewayimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.PageQuery;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nidu.demo.role.dto.RoleListQry;
import com.nidu.demo.role.dto.RolePageQry;
import com.nidu.demo.role.convertor.RoleConvertor;
import com.nidu.demo.role.gateway.RoleGateway;
import com.nidu.demo.role.mapper.RoleMapper;
import com.nidu.demo.role.mapper.dataobject.RoleDO;
import com.nidu.demo.role.model.Role;
import com.nidu.demo.role.model.RoleCriteria;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Set;
import java.util.List;

@Component
@RequiredArgsConstructor
public class RoleGatewayImpl implements RoleGateway {
    
    private final RoleMapper mapper;
    private final RoleConvertor convertor;

    @Override
    public Role create(Role role) {
        RoleDO roleDO = convertor.toDataObject(role);
        mapper.insert(roleDO);
        return convertor.toDomainObject(roleDO);
    }

    @Override
    public Role update(Role role) {
        RoleDO roleDO = convertor.toDataObject(role);
        mapper.updateById(roleDO);
        return convertor.toDomainObject(roleDO);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteById(id);
    }

    @Override
    public void deleteByIds(Set<Long> ids) {
        mapper.deleteByIds(ids);
    }

    @Override
    public Role getById(Long id) {
        RoleDO roleDO = mapper.selectById(id);
        if(roleDO == null){
            return null;
        }
        return convertor.toDomainObject(roleDO);
    }

    @Override
    public List<Role> getByIds(Set<Long> ids) {
        LambdaQueryWrapper<RoleDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(RoleDO::getId, ids);
        List<RoleDO> roleDOS = mapper.selectList(wrapper);
        if(CollUtil.isEmpty(roleDOS)){
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(roleDOS);
    }

    @Override
    public Role getByCode(String code) {
        RoleDO roleDO = mapper.selectByCode(code);
        if(roleDO == null){
            return null;
        }
        return convertor.toDomainObject(roleDO);
    }

    @Override
    public List<Role> listByCondition(RoleCriteria criteria) {
        LambdaQueryWrapper<RoleDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(criteria.getStatus() != null, RoleDO::getStatus, criteria.getStatus());
        wrapper.in(!CollUtil.isEmpty(criteria.getRoleIds()), RoleDO::getId, criteria.getRoleIds());

        wrapper.eq(!StrUtil.isEmpty(criteria.getRoleCode()), RoleDO::getCode, criteria.getRoleCode());
        wrapper.like(!StrUtil.isEmpty(criteria.getRoleName()), RoleDO::getName, criteria.getRoleName());
        List<RoleDO> roleDOS = mapper.selectList(wrapper);
        if(CollUtil.isEmpty(roleDOS)){
            return new ArrayList<>();
        }
        return convertor.toDomainObjects(roleDOS);
    }

    @Override
    public PageResponse<Role> pageByCondition(RoleCriteria criteria) {
        // 创建查询条件
        LambdaQueryWrapper<RoleDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(criteria.getStatus() != null, RoleDO::getStatus, criteria.getStatus());

        LocalDateTime[] createTime = criteria.getCreateTime();
        if (createTime != null) {
            if (createTime.length == 1 && createTime[0] != null) {
                wrapper.ge(RoleDO::getCreateTime, createTime[0]);
            } else if (createTime.length > 1 && createTime[0] != null && createTime[1] != null) {
                wrapper.between(RoleDO::getCreateTime, createTime[0], createTime[1]);
            }
        }

        wrapper.eq(!StrUtil.isEmpty(criteria.getRoleCode()), RoleDO::getCode, criteria.getRoleCode());
        wrapper.like(!StrUtil.isEmpty(criteria.getRoleName()), RoleDO::getName, criteria.getRoleName());

        // 创建分页对象，确保使用 RoleDO
        Page<RoleDO> page = new Page<>(criteria.getPageIndex(), criteria.getPageSize());

        // 排序字段
        if (!StrUtil.isEmpty(criteria.getOrderBy())) {
            if (PageQuery.DESC.equals(criteria.getOrderDirection())) {
                page.addOrder(OrderItem.desc(criteria.getOrderBy()));
            } else {
                page.addOrder(OrderItem.asc(criteria.getOrderBy()));
            }
        }
        mapper.selectPage(page, wrapper);
        List<Role> roles = convertor.toDomainObjects(page.getRecords());
        return PageResponse.of(roles, (int)page.getTotal(), (int)page.getSize(), (int)page.getCurrent());
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        RoleDO roleDO = new RoleDO();
        roleDO.setId(id);
        roleDO.setStatus(status);
        mapper.updateById(roleDO);
    }
} 